————————————————————————————————————————————————————————————————————————————————————————————————
  nginx 配置geoip 屏蔽地区城市，实现判断国家IP跳转
————————————————————————————————————————————————————————————————————————————————————————————————
步骤    ./configure  --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-ipv6 --with-http_sub_module --with-http_flv_module --user=www --group=www --with-http_gzip_static_module --with-http_geoip_module

相应的省份代码：

CN,01,”Anhui”
CN,02,”Zhejiang”
CN,03,”Jiangxi”
CN,04,”Jiangsu”
CN,05,”Jilin”
CN,06,”Qinghai”
CN,07,”Fujian”
CN,08,”Heilongjiang”
CN,09,”Henan”
CN,10,”Hebei”
CN,11,”Hunan”
CN,12,”Hubei”
CN,13,”Xinjiang”
CN,14,”Xizang”
CN,15,”Gansu”
CN,16,”Guangxi”
CN,18,”Guizhou”
CN,19,”Liaoning”
CN,20,”Nei Mongol”
CN,21,”Ningxia”
CN,22,”Beijing”
CN,23,”Shanghai”
CN,24,”Shanxi”
CN,25,”Shandong”
CN,26,”Shaanxi”
CN,28,”Tianjin”
CN,29,”Yunnan”
CN,30,”Guangdong”
CN,31,”Hainan”
CN,32,”Sichuan”
CN,33,”Chongqing”

安装 Nginx
因为要用到 http_geoip_module 模块，系统自带的 nginx 一般不带这个模块，所以要下载 nginx 源代码后自行编译：

# wget http://nginx.org/download/nginx-0.9.6.tar.gz
# tar zxvf nginx-0.9.6.tar.gz
# cd nginx-0.9.6
# ./configure --without-http_empty_gif_module --with-poll_module \
--with-http_stub_status_module --with-http_ssl_module \
--with-http_geoip_module
# make; make install


安装 MaxMind 的 GeoIP 库
MaxMind 提供了免费的 IP 地域数据库（GeoIP.dat），不过这个数据库文件是二进制的，需要用 GeoIP 库来读取，所以除了要下载 GeoIP.dat 文件外（见下一步），还需要安装能读取这个文件的库。

# wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz
# tar -zxvf GeoIP.tar.gz
# cd GeoIP-1.4.6
# ./configure
# make; make install
刚才安装的库自动安装到 /usr/local/lib 下，所以这个目录需要加到动态链接配置里面以便运行相关程序的时候能自动绑定到这个 GeoIP 库：

# echo '/usr/local/lib' > /etc/ld.so.conf.d/geoip.conf
# ldconfig


下载 IP 数据库
MaxMind 提供了免费的 IP 地域数据库，这个数据库是二进制的，不能用文本编辑器打开，需要上面的 GeoIP 库来读取：

# wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
# gunzip GeoIP.dat.gz


配置 Nginx
最后是配置 nginx，在相关地方加上如下的配置就可以了：

# vi /etc/nginx/nginx.conf
...
geoip_country /home/vpsee/GeoIP.dat;

fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code;
fastcgi_param GEOIP_COUNTRY_CODE3 $geoip_country_code3;
fastcgi_param GEOIP_COUNTRY_NAME $geoip_country_name;
...

 if ($geoip_country_code = CN) {
    root /home/vpsee/cn/;
 }


这样，当来自中国的 IP 访问网站后就自动访问到预定的 /home/vpsee/cn 页面。关于 Nginx + GeoIP 还有很多有用的用法，比如做个简单的 CDN，来自中国的访问自动解析到国内服务器、来自美国的访问自动转向到美国服务器等。MaxMind 还提供了全球各个城市的 IP 信息，还可以下载城市 IP 数据库来针对不同城市做处理。

————————————————————————————————————————————————————————————————————————————————————————————————
  nginx 配置geoip 屏蔽地区城市，实现判断国家IP跳转
————————————————————————————————————————————————————————————————————————————————————————————————

geoip_module模块
基于ip地址匹配MaxMind GeoIp 二进制文件,读取ip所在地域信息。

vim /etc/yum.repos.d/nginx.repo

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

sudo 
yum-config-manager --enable nginx-mainline

安装
    # yum 安装nginx geoip模块
    yum install nginx-module-geoip

    # 下载geoIP 地域文件
    wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
    wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
使用场景
区别国内外作HTTP访问规则
区别国内城市地域作HTTP访问规则
例子:
    # nginx.conf 加载第三方的模块
    load_module "modules/ngx_http_geoip_module.so";
    load_module "modules/ngx_stream_geoip_module.so";

    ...

    # 之前下载的GeoIP地域文件地址
    geoip_country /etc/nginx/geoip/GeoIP.dat;
    geoip_city /etc/nginx/geoip/GeoLiteCity.dat;

    server{
        ...

        location / {
            # 如果不是国内的服务器 就返回403
            if ($geoip_country_code != CN) {
                return 403;
            }

            root /usr/share/nginx/html;
            index index.php index.html index.htm
        }

        # 获取当前访问的IP
        location /myIp {
            default_type text/plain;
            return 200 "$remote_addr $geoip_country_name $geoip_country_code $geoip_city";
            # 输出 1.198.216.102 China CN ZhengZhou
        }
        ...
    }

    ...